<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>借阅信息表</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />

    <script src="../../scripts/boot.js" type="text/javascript"></script>
</head>

<body>
    <div style="width: 100%;">
        <div class="mini-toolbar" style="border-bottom: 0; padding: 0px;">
            <table style="width: 100%;">
                <tr>
                    <td style="width: 100%;">
                        <a class="mini-button" iconCls="icon-add" onclick="add()">增加</a>
                    </td>

                </tr>
            </table>
        </div>
    </div>
    <div id="borrowlist" class="mini-datagrid" style="width: 100%; height: auto;" url="../../borrowinfolistaction"
        idField="id" multiSelect="true">
        <div property="columns">
            <div type="checkcolumn"></div>
            <div field="borrowId" width="120" headerAlign="center" allowSort="true">借阅号</div>
            <div field="bookName" width="120" headerAlign="center" allowSort="true">图书名称</div>
            <div field="borrower" width="100" headerAlign="center" allowSort="true">借阅人</div>
            <div field="phone" width="100" headerAlign="center" allowSort="true">联系电话</div>
            <div field="borrowTime" width="120" headerAlign="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm:ss">图书借阅时间
            </div>

            <div field="returnTime" width="120" headerAlign="center" allowSort="true" dateFormat="yyyy-MM-dd HH:mm:ss">归还时间</div>
            <div name="action" width="120" headerAlign="center" renderer="onActionRenderer" cellStyle="padding:0;">操作
            </div>

        </div>
    </div>


    <script type="text/javascript">
        mini.parse();

        var grid = mini.get("borrowlist");
        grid.load();
        grid.sortBy("borrowTime", "desc");

        /* 操作按钮 */
        function onActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._id;
            var rowIndex = e.rowIndex;

            var s = "";
            if (record.returnTime != undefined && record.returnTime != null) {
                s = '<a class="mini-button" onclick="delRow()">删除</a>';
            } else {
                s = '<a class="mini-button" onclick="returnBook()">还书</a>';
            }

            return s;
        }

        // 新增借阅信息
        function add() {

            mini.open({
                url: "pages/borrowinfo/borrowinfoadd.html",
                title: "新增借阅信息",
                width: 500,
                height: 350,
                allowResize: false,
                onload: function () {
                    var iframe = this.getIFrameEl();
                },
                ondestroy: function (action) {
                    grid.reload();
                }
            });
        }
        // 还书操作
        function returnBook() {
            var row = grid.getSelected();
            var nowDate = mini.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss");
            var json = mini.encode(row);
            mini.confirm("确定还书？", "确定？",
                function (action) {
                    if (action == "ok") {
                        grid.loading("操作中，请稍后......");
                        $.ajax({
                            url: "../../borrowinfolistaction",
                            type: "post",
                            data: {
                                method: "updateRemain",
                                data: json,
                                returnTime: nowDate
                            },
                            success: function (text) {
                                var res = mini.decode(text);
                                showTips(res);
                                grid.reload();
                            },
                            error: function () {
                            }
                        });
                    }
                });
        }
       
        // 删除记录
        function delRow() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                mini.confirm("确定删除记录？", "确定？",
                    function (action) {
                        if (action == "ok") {
                            var ids = [];
                            for (var i = 0, l = rows.length; i < l; i++) {
                                var r = rows[i];
                                ids.push(r.borrowId);
                            }
                            var id = ids.join(',');
                            grid.loading("操作中，请稍后......");
                            $.ajax({
                                url: "../../borrowinfodelaction",
                                data: {
                                    ids: id
                                },
                                success: function (text) {
                                    var res = mini.decode(text);
                                    showTips(res);
                                    grid.reload();
                                },
                                error: function () {
                                }
                            });
                        }
                    }
                );
            } else {
                alert("请选中一条记录");
            }
        }
        // 信息提示
        function showTips(res) {
            if (res.code == 1) {
                mini.showTips({
                    content: "<b>" + res.msg + "</b>",
                    x: "center",
                    y: "top",
                    state: "success",
                    timeout: 3000
                });
            } else {
                mini.showTips({
                    content: "<b>" + res.msg + "</b>",
                    x: "center",
                    y: "top",
                    state: "danger",
                    timeout: 3000
                });
            }
        }
    </script>

</body>

</html>